from channels.generic.websocket import  JsonWebsocketConsumer

import psutil
from utils.rest.decorator import action_name
from utils.time import get_current_time_by_microsecond


@action_name('设备实时信息')
class DeviceInfoRealTimeInfoConsumer(JsonWebsocketConsumer):

    def websocket_connect(self, event):
        self.accept()

    def websocket_receive(self, event):
        cpu_usage = psutil.cpu_percent()
        memory_usage = psutil.virtual_memory().percent
        self.send_json({
            'cpu': cpu_usage,
            'memory': memory_usage,
            'time': get_current_time_by_microsecond()
        })


